//
//  CircleProgressView.m
//  Learn
//
//  Created by zyl on 2024/6/3.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "CircleProgressView.h"

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
@interface CircleProgressView ()

/**
 导轨layer
 */
@property (nonatomic, strong) CAShapeLayer *circleBackgroundLayer;

/**
 进度layer
 */
@property (nonatomic, strong) CAShapeLayer *progressLayer;

/**
 半径
 */
@property (nonatomic, assign) CGFloat cornerRadius;

@end

@implementation CircleProgressView


- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor clearColor];
        _trackColor = [UIColor redColor];
        _lineWidth = 10.0;
        _progressColor = [UIColor redColor];
        _progressLineCap = kCALineCapRound;
     
//        _contentText = @"";
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.cornerRadius = MIN(self.width, self.height) * 0.5 - self.lineWidth * 0.5;

}

#pragma mark - Draw

- (void)drawRect:(CGRect)rect {
    // Drawing code
}


- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    [super drawLayer:layer inContext:ctx];
    [self fl_drawCircleBackgroundLayer];
    [self fl_drawProgressLayer];
   
}

- (void)fl_drawCircleBackgroundLayer {
    //从 π/2 开始绘制一个圆形导轨
    CGPoint center = CGPointMake(self.width * 0.5, self.height * 0.5);
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:center radius:self.cornerRadius startAngle:M_PI / 2 endAngle:2 * M_PI + M_PI / 2 clockwise:YES];
    self.circleBackgroundLayer.path = circlePath.CGPath;
    
    [self.layer addSublayer:self.circleBackgroundLayer];
}

- (void)fl_drawProgressLayer {
    //从 π/2 开始绘制一个进度
    CGPoint center = CGPointMake(self.width * 0.5, self.height * 0.5);
    UIBezierPath *progressPath = [UIBezierPath bezierPathWithArcCenter:center radius:self.cornerRadius startAngle:0 endAngle:2 * M_PI clockwise:YES];
    
    self.progressLayer.path = progressPath.CGPath;
    self.progressLayer.strokeStart = 0.0;
    self.progressLayer.strokeEnd = self.progress;
    
    [self.circleBackgroundLayer addSublayer:self.progressLayer];
}

#pragma mark - Setter

- (void)setTrackColor:(UIColor *)trackColor {
    _trackColor = trackColor;
    self.circleBackgroundLayer.strokeColor = trackColor.CGColor;
}

- (void)setLineWidth:(CGFloat)lineWidth {
    _lineWidth = lineWidth;
    self.circleBackgroundLayer.lineWidth = lineWidth;
    self.progressLayer.lineWidth = lineWidth;
}

- (void)setProgress:(CGFloat)progress {
    
    _progress = progress;
    self.progressLayer.strokeEnd = progress;
   
}

- (void)setProgressColor:(UIColor *)progressColor {
    _progressColor = progressColor;
    self.progressLayer.strokeColor = progressColor.CGColor;
}

- (void)setProgressLineCap:(CAShapeLayerLineCap)progressLineCap {
    _progressLineCap = progressLineCap;
    self.progressLayer.lineCap = progressLineCap;
}

- (void)setStartPoint:(CGFloat)startPoint
{
    _startPoint = startPoint;
    CGPoint center = CGPointMake(self.width * 0.5, self.height * 0.5);
    UIBezierPath *progressPath = [UIBezierPath bezierPathWithArcCenter:center radius:self.cornerRadius startAngle:DEGREES_TO_RADIANS(self.startPoint) endAngle:2 * M_PI + DEGREES_TO_RADIANS(self.startPoint) clockwise:YES];
    self.progressLayer.path = progressPath.CGPath;
    
}

#pragma mark - lazy load.

- (CAShapeLayer *)circleBackgroundLayer {
    if (!_circleBackgroundLayer) {
        _circleBackgroundLayer = [CAShapeLayer layer];
        _circleBackgroundLayer.lineWidth = self.lineWidth;
        _circleBackgroundLayer.strokeColor = self.trackColor.CGColor;
        _circleBackgroundLayer.fillColor = [UIColor clearColor].CGColor;
        _circleBackgroundLayer.lineCap = kCALineCapButt;
    }
    return _circleBackgroundLayer;
}

- (CAShapeLayer *)progressLayer {
    if (!_progressLayer) {
        _progressLayer = [CAShapeLayer layer];
        _progressLayer.lineWidth = self.lineWidth;
        _progressLayer.strokeColor = self.progressColor.CGColor;
        _progressLayer.fillColor = [UIColor clearColor].CGColor;
        _progressLayer.lineCap = self.progressLineCap;
    }
    return _progressLayer;
}


@end
